WhereRelation() vs whereHas()

  • STEP

    users:

    countries:

    app/Models/User.php

    
    
                    namespace App\Models;
      
                    use Illuminate\Contracts\Auth\MustVerifyEmail;
                    use Illuminate\Database\Eloquent\Factories\HasFactory;
                    use Illuminate\Foundation\Auth\User as Authenticatable;
                    use Illuminate\Notifications\Notifiable;
                    use Laravel\Sanctum\HasApiTokens;
                    
                    class User extends Authenticatable
                    {
                        use HasApiTokens, HasFactory, Notifiable;
                    
                        /**
                        * The attributes that are mass assignable.
                        *
                        * @var  string[]
                        */
                        protected $fillable = [
                            'name',
                            'email',
                            'password',
                        ]; 
                    
                        /**
                        * The attributes that should be hidden for serialization.
                        *
                        * @var  array
                        */
                        protected $hidden = [
                            'password',
                            'remember_token',
                        ];
                    
                        /**
                        * The attributes that should be cast.
                        *
                        * @var  array
                        */
                        protected $casts = [
                            'email_verified_at' => 'datetime',
                        ];
                    
                        /**
                        * Get the comments for the blog post.
                        */
                        public function country()
                        {
                            return $this->belongsTo(Country::class);
                        }
                    }
    
                    

    app/Models/Country.php

    
                    namespace App\Models;
      
                    use Illuminate\Database\Eloquent\Factories\HasFactory;
                    use Illuminate\Database\Eloquent\Model;
                        
                    class Country extends Model
                    {
                        use HasFactory;
                    }
    
                    

    using whereRelation()

    
                    namespace App\Http\Controllers;
      
      use Illuminate\Http\Request;
      use App\Models\User;
        
      class DemoController extends Controller
      {
          /**
           * Write code on Method
           *
           * @return  response()
           */
          public function index()
          {
              $name = 'india';
              $users = User::with('country')
                              ->whereRelation('country', 'name', 'like', '%'.$name.'%')
                              ->get()
                              ->toArray();
        
              dd($users);
          }
      }
    
    
    Output
    
    Array
    
    (
    
        [0] => Array
    
            (
    
                [id] => 1
    
                [name] => Hardik Savani
    
                [country_id] => 2
    
                [email] => savanihd@gmail.com
    
                [email_verified_at] => 
    
                [two_factor_secret] => 
    
                [two_factor_recovery_codes] => 
    
                [current_team_id] => 
    
                [profile_photo_path] => 
    
                [created_at] => 2020-09-12T06:46:08.000000Z
    
                [updated_at] => 2020-09-18T12:04:09.000000Z
    
                [deleted_at] => 
    
                [country] => Array
    
                    (
    
                        [id] => 2
    
                        [name] => india
    
                        [code] => 2
    
                        [created_at] => 2021-08-09T14:58:47.000000Z
    
                        [updated_at] => 2021-08-09T14:58:47.000000Z
    
                    )
    
            )
    
    )
    
                    

    Using whereHas()

    
    
                    public function index()
    {
        $name = 'india';
        $users = User::with('country')
                        ->whereHas('country', function (Builder $query) use($name){
                            $query->where('name', 'like', '%'.$name.'%');
                        })
                        ->get()
                        ->toArray();
      
        dd($users);
    }